package com.pro_one.service.impl;

import com.pro_one.dao.MemberDao;
import com.pro_one.dao.OrderDao;
import com.pro_one.dao.SetmealDao;
import com.pro_one.service.ReportService;
import com.pro_one.util.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 查询所有运营数据
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        //今天
        String today = DateUtils.parseDate2String(new Date());
        //周一
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //每月第一天
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //今日新增会员
        int todayNewMember = memberDao.getTodayNewMember(today);
        //总会员
        int totalMember = memberDao.getTotalMember();
        //本周新增会员
        int thisWeekNewMember = memberDao.getMemberCountAfterDate(thisWeekMonday);
        //本月新增会员
        int thisMonthNewMember = memberDao.getMemberCountAfterDate(firstDay4ThisMonth);
        //今日预约人数
        int todayOrderNumber = orderDao.getOrderCountByDate(today);
        //今日已到诊人数
        int todayVisitsNumber = orderDao.getTodayVisitsNumber(today);
        //本周预约数
        int thisWeekOrderNumber = orderDao.getOrderCountAfterDate(thisWeekMonday);
        //本周已到诊人数
        int thisWeekVisitsNumber = orderDao.getVisitsNumberAfterDate(thisWeekMonday);
        //本月预约人数
        int thisMonthOrderNumber = orderDao.getOrderCountAfterDate(firstDay4ThisMonth);
        //本月已到诊人数
        int thisMonthVisitsNumber = orderDao.getVisitsNumberAfterDate(firstDay4ThisMonth);
        //热门套餐
        List<Map> hotSetmeal = setmealDao.findHotSetmeal();
        for (Map map : hotSetmeal) {
            BigDecimal proportion = (BigDecimal) map.get("proportion");
            map.put("proportion", proportion.doubleValue() + "%");
        }

        Map<String, Object> reportData = new HashMap<>();
        reportData.put("reportDate", today);
        reportData.put("todayNewMember", todayNewMember);
        reportData.put("totalMember", totalMember);
        reportData.put("thisWeekNewMember", thisWeekNewMember);
        reportData.put("thisMonthNewMember", thisMonthNewMember);
        reportData.put("todayOrderNumber", todayOrderNumber);
        reportData.put("todayVisitsNumber", todayVisitsNumber);
        reportData.put("thisWeekOrderNumber", thisWeekOrderNumber);
        reportData.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        reportData.put("thisMonthOrderNumber", thisMonthOrderNumber);
        reportData.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        reportData.put("hotSetmeal", hotSetmeal);
        return reportData;
    }
}
